/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yixiu.modules.db.druid;

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.yixiu.RenrenLearningApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.sql.*;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * druid 多数据源测试类
 */
@RunWith(SpringRunner.class)
@SpringBootTest()
// @Profile 注解说明这个类只会读取 名字中带有"multi-datasource"的资源文件
// @ActiveProfiles("multi-datasource") 注解说明激活 名字中带有"multi-datasource"的资源文件
// 这两个要搭配在一起使用。也可以通过在配置文件中指定spring.profiles.active来激活文件。
@ActiveProfiles("multi-datasource")
public class MultiDataSourceTestCase {

    //  【@Resource的效果】=【@Autowried】 + 【@Qualifier】
    //  @Autowired   按照类型获取bean;
    //  @Qualifier  当有多个类型的bean时，按照名字搜索
    @Autowired
    @Qualifier("dataSourceOne")  // @Resource 注解的意思是 autowire by name，按照名字从容器中查找bean
    private DruidDataSource dataSourceOne;

    //  @Resource 按照名称获取bean
    @Resource
    private DruidDataSource dataSourceTwo;

    @Test
    public void testDataSourceOne() throws SQLException {

        assertThat(dataSourceOne.getUrl()).isEqualTo("jdbc:mysql://localhost:3306/renren_security?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        assertThat(dataSourceOne.getUsername()).isEqualTo("root");
        assertThat(dataSourceOne.getPassword()).isEqualTo("123456");
        assertThat(dataSourceOne.getDriverClassName()).isEqualTo("com.mysql.jdbc.Driver");

        assertThat(dataSourceOne.getInitialSize()).isEqualTo(5);

        assertThat(dataSourceOne.getMaxActive()).isEqualTo(10);
        assertThat(dataSourceOne.getMaxWait()).isEqualTo(10000);
    }
    @Test
    public void testDataSourceTwo() throws SQLException {

        assertThat(dataSourceTwo.getUrl()).isEqualTo("jdbc:mysql://localhost:3306/mysql?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        assertThat(dataSourceTwo.getUsername()).isEqualTo("root");
        assertThat(dataSourceTwo.getPassword()).isEqualTo("123456");
        assertThat(dataSourceTwo.getDriverClassName()).isEqualTo("com.mysql.jdbc.Driver");

        assertThat(dataSourceTwo.getInitialSize()).isEqualTo(5);

        assertThat(dataSourceTwo.getMaxActive()).isEqualTo(20);
        assertThat(dataSourceTwo.getMaxWait()).isEqualTo(20000);
    }

    // 测试2个数据源
    @Test
    public void testDataSource() throws  Exception {

        search(dataSourceOne);

        search(dataSourceTwo);
    }


    /**
     * 根据数据源，查询一个表的所有数据
     * 通过基本的jdbc操作测试
     * @param druidDataSource
     * @throws SQLException
     */
    private  void search(DruidDataSource druidDataSource) throws SQLException {

        Connection connection = druidDataSource.getConnection(); // 从连接池获取连接

        // 查询语句
        Statement statement = connection.createStatement();
        String sql = "select * from user";
        ResultSet resultSet = statement.executeQuery(sql);

        // 输出内容
        ResultSetMetaData md = resultSet.getMetaData(); //获得结果集结构信息,元数据
        int columnCount = md.getColumnCount();   //获得列数
        for (int i = 1; i <= columnCount; i++) {
            System.out.printf("%5s    " , md.getColumnName(i));
           // , resultSet.getObject(i);
        }
        System.out.print("\n");

        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                System.out.printf("%5s    " , resultSet.getObject(i) );
            }
            System.out.print("\n");
        }


        resultSet.close();
        statement.close();
        connection.close();
        //druidDataSource.close();

    }



}
